﻿package frEngine.primitives
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import flash.geom.*;
    import frEngine.animateControler.cylinderControler.*;
    import frEngine.animateControler.keyframe.*;
    import frEngine.core.*;
    import frEngine.pools.*;
    import frEngine.render.*;
    import frEngine.render.layer.*;
    import frEngine.shader.filters.*;
    import frEngine.shader.filters.fragmentFilters.*;
    import frEngine.shader.filters.vertexFilters.*;

    public class FrAnimCylinder extends Mesh3D
    {
        private var _topR:Number;
        private var _bottomR:Number;
        private var _segments:int;
        private var _h:Number;
        private var _surface:FrSurface3D;

        public function FrAnimCylinder(param1:String)
        {
            super(param1, true, null);
            this._surface = new FrSurface3D("cylinder");
            this._surface.addVertexData(FilterName_ID.POSITION_ID, 4, false, null);
            this._surface.addVertexData(FilterName_ID.UV_ID, 2, false, null);
            this.render = CylinderRender.instance;
            this.bounds = new Boundings3D();
            this.setMateiralBlendMode(EBlendType.BLEND_LIGHT);
            this.material.materialParams.depthWrite = false;
            this.material.materialParams.twoSided = true;
            this.setLayer(Layer3DManager.AlphaLayer0);
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            super.dispose(param1);
            this._surface.download();
            return;
        }// end function

        public function reinit(param1:RenderList, param2:Number = 10, param3:Number = 10, param4:Number = 10, param5:int = 4, param6 = null, param7:Boolean = false) : void
        {
            this.renderList = param1;
            if (!param6)
            {
                param6 = 13421772;
            }
            this._segments = param5;
            this._topR = param2;
            this._bottomR = param3;
            this._h = param4;
            this.setSurface(0, this._surface);
            this._surface.getVertexBufferByNameId(FilterName_ID.POSITION_ID).vertexVector = this.getVertexVector(param7);
            this._surface.indexVector = this.getIndexVector();
            var _loc_8:* = Math.max(this._topR, this._bottomR);
            var _loc_9:* = Math.PI / this._segments;
            _loc_8 = Math.cos(_loc_9) * _loc_8;
            this.bounds.min.setTo(-_loc_8, 0, -_loc_8);
            this.bounds.max.setTo(_loc_8, this._h, _loc_8);
            this.bounds.length.x = this.bounds.max.x - this.bounds.min.x;
            this.bounds.length.y = this._h;
            this.bounds.length.z = this.bounds.max.z - this.bounds.min.z;
            this.bounds.center.x = this.bounds.length.x * 0.5 + this.bounds.min.x;
            this.bounds.center.y = this.bounds.length.y * 0.5 + this.bounds.min.y;
            this.bounds.center.z = this.bounds.length.z * 0.5 + this.bounds.min.z;
            this.bounds.radius = Vector3D.distance(this.bounds.center, this.bounds.max);
            setMaterial(param6, Texture3D.MIP_NONE, "frAnimCylinder");
            return;
        }// end function

        override public function getAnimateControlerInstance(param1:int, param2:Modifier = null) : Modifier
        {
            var _loc_3:* = super.getAnimateControlerInstance(param1, param2);
            if (_loc_3 == null)
            {
                switch(param1)
                {
                    case AnimateControlerType.CylinderUpRadiusControler:
                    {
                        _loc_3 = param2 ? (param2) : (FrObjectPool.getObject(CylinderUpRadiusControler, this));
                        break;
                    }
                    case AnimateControlerType.CylinderDownRadiusControler:
                    {
                        _loc_3 = param2 ? (param2) : (FrObjectPool.getObject(CylinderDownRadiusControler, this));
                        break;
                    }
                    case AnimateControlerType.CylinderHControler:
                    {
                        _loc_3 = param2 ? (param2) : (FrObjectPool.getObject(CylinderHControler, this));
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
                if (_loc_3)
                {
                    _animateControlerList[param1] = _loc_3;
                    _loc_3.targetObject3d = this;
                }
            }
            return _loc_3;
        }// end function

        override public function setShaderBase(param1:VertexFilter, param2:FragmentFilter) : void
        {
            super.setShaderBase(new CylinderAnimaterVertexFilter(this._topR, this._bottomR, this._h), param2);
            return;
        }// end function

        private function getVertexVector(param1:Boolean) : Vector.<Number>
        {
            var _loc_5:int = 0;
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_2:* = new Vector.<Number>;
            var _loc_3:* = Math.PI * 2 / this._segments;
            var _loc_4:* = 1 / this._segments;
            if (param1)
            {
                _loc_5 = 0;
                while (_loc_5 <= this._segments)
                {
                    
                    _loc_6 = _loc_3 * _loc_5;
                    _loc_7 = Math.cos(_loc_6) * 0.5 + 0.5;
                    _loc_8 = Math.sin(_loc_6) * 0.5 + 0.5;
                    _loc_2.push(1, 1, 0, _loc_6, _loc_7, _loc_8);
                    _loc_7 = Math.cos(_loc_6) * 0.01 + 0.5;
                    _loc_8 = Math.sin(_loc_6) * 0.01 + 0.5;
                    _loc_2.push(0, 0, 1, _loc_6, _loc_7, _loc_8);
                    _loc_5++;
                }
            }
            else
            {
                _loc_5 = 0;
                while (_loc_5 <= this._segments)
                {
                    
                    _loc_6 = _loc_3 * _loc_5;
                    _loc_9 = _loc_4 * _loc_5;
                    _loc_2.push(1, 1, 0, _loc_6, _loc_9, 0);
                    _loc_2.push(0, 0, 1, _loc_6, _loc_9, 1);
                    _loc_5++;
                }
            }
            return _loc_2;
        }// end function

        private function getIndexVector() : Vector.<uint>
        {
            var _loc_3:int = 0;
            var _loc_1:* = new Vector.<uint>;
            var _loc_2:int = 0;
            while (_loc_2 < this._segments)
            {
                
                _loc_3 = _loc_2 * 2;
                _loc_1.push(_loc_3, _loc_3 + 3, (_loc_3 + 1));
                _loc_1.push(_loc_3 + 3, _loc_3, _loc_3 + 2);
                _loc_2++;
            }
            return _loc_1;
        }// end function

        public function get segments() : int
        {
            return this._segments;
        }// end function

        public function get topR() : Number
        {
            return this._topR;
        }// end function

        public function get bottomR() : Number
        {
            return this._bottomR;
        }// end function

        public function get height() : Number
        {
            return this._h;
        }// end function

    }
}
